package com.happok.onlineclass.usermanagement.setting.service.impl;

import com.happok.onlineclass.usermanagement.common.exception.DisableException;
import com.happok.onlineclass.usermanagement.setting.dto.*;
import com.happok.onlineclass.usermanagement.setting.po.FunctionSettingPO;
import com.happok.onlineclass.usermanagement.setting.service.EmailService;
import com.happok.onlineclass.usermanagement.setting.service.FunctionSettingDaoService;
import com.happok.onlineclass.usermanagement.setting.service.FunctionSettingService;
import com.happok.onlineclass.usermanagement.setting.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/10/12 11:25
 */
@Slf4j
@Service
public class FunctionSettingServiceImpl implements FunctionSettingService {

    @Autowired
    private SmsService smsService;

    @Autowired
    private EmailService emailService;

    @Autowired
    private FunctionSettingDaoService functionSettingDaoService;

    @Override
    public void openOrCloseUserRegister(UserRegisterStatusDTO userRegisterStatusDTO) {
        // 检测邮箱以及短信服务器都是配置正常，如果都配置不正常则开启异常
        if (userRegisterStatusDTO.getOpen()) {
            boolean emailStatus = true;
            try {
                emailService.checkAndConnectEmail();
            } catch (Exception emailException) {
                log.error("邮箱服务异常");
                emailStatus = false;
            }
            boolean smsStatus = true;
            try {
                smsService.checkAndConnectSms();
            } catch (Exception smsException) {
                log.error("短信服务异常");
                smsStatus = false;
            }
            if (!emailStatus && !smsStatus) {
                throw new DisableException("设置异常");
            }
        }
        // 开启用户注册
        functionSettingDaoService.openOrCloseFunction(FunctionClassifyEnum.USER_REGIST, userRegisterStatusDTO.getOpen());
    }

    @Override
    public void updateFunctionSetting(FunctionClassifyEnum functionClassify, UpdateVerifyDTO updateVerify) {
        if (!ObjectUtils.isEmpty(updateVerify.getEmailVerify()) &&
                !ObjectUtils.isEmpty(updateVerify.getEmailVerify().getTopic()) &&
                !ObjectUtils.isEmpty(updateVerify.getEmailVerify().getContent())) {
            emailService.checkAndConnectEmail();
        }
        if (!ObjectUtils.isEmpty(updateVerify.getSmsVerify()) &&
                !ObjectUtils.isEmpty(updateVerify.getSmsVerify().getTemplateCode())) {
            smsService.checkAndConnectSms();
        }
        functionSettingDaoService.updateFunctionSetting(functionClassify, updateVerify);
    }

    @Override
    public FunctionVerifyDTO getFunctionSetting(FunctionClassifyEnum functionClassify) {
        FunctionSettingPO functionSettingPO = functionSettingDaoService.getFunctionSetting(functionClassify);
        FunctionVerifyDTO functionVerifyDTO = new FunctionVerifyDTO();
        EmailVerifyDTO emailVerify = new EmailVerifyDTO();
        SmsVerifyDTO smsVerify = new SmsVerifyDTO();
        if (!ObjectUtils.isEmpty(functionSettingPO)) {
            BeanUtils.copyProperties(functionSettingPO, emailVerify);
            BeanUtils.copyProperties(functionSettingPO, smsVerify);
            functionVerifyDTO.setOpen(functionSettingPO.getOpen());
        } else {
            functionVerifyDTO.setOpen(false);
        }
        functionVerifyDTO.setSmsVerify(smsVerify);
        functionVerifyDTO.setEmailVerify(emailVerify);
        functionVerifyDTO.setFunctionClassify(functionClassify);
        return functionVerifyDTO;
    }

    @Override
    public List<FunctionVerifyDTO> listFunctionSetting() {
        List<FunctionSettingPO> functionSettingPOS = functionSettingDaoService.listFunctionSetting();
        List<FunctionVerifyDTO> functionVerifyDTOS = new ArrayList<>();
        for (FunctionSettingPO functionSettingPO : functionSettingPOS) {
            FunctionVerifyDTO functionVerifyDTO = new FunctionVerifyDTO();
            EmailVerifyDTO emailVerify = new EmailVerifyDTO();
            SmsVerifyDTO smsVerify = new SmsVerifyDTO();
            if (!ObjectUtils.isEmpty(functionSettingPO)) {
                BeanUtils.copyProperties(functionSettingPO, emailVerify);
                BeanUtils.copyProperties(functionSettingPO, smsVerify);
                BeanUtils.copyProperties(functionSettingPO, functionVerifyDTO);
            } else {
                functionVerifyDTO.setOpen(false);
            }
            functionVerifyDTO.setSmsVerify(smsVerify);
            functionVerifyDTO.setEmailVerify(emailVerify);
            functionVerifyDTOS.add(functionVerifyDTO);
        }
        return functionVerifyDTOS;
    }
}